package com.tsf.hbase.util;

import org.apache.hadoop.hbase.util.Base64;
import org.springframework.util.StringUtils;

import java.nio.charset.Charset;

public final class HBaseUtils {

    public static Charset getCharset(String encoding) {
        return (StringUtils.hasText(encoding) ? Charset.forName(encoding) : Charset.forName("UTF-8"));
    }

    public static String replace(String rowKeyAsString) {
        rowKeyAsString = rowKeyAsString.replace("\\n", "");
        rowKeyAsString = rowKeyAsString.replace(" ", "+");
        return rowKeyAsString;
    }

    /**
     * 这里不能直接使用前端给的字符串，否则还原成bytes时会有问题
     */
    public static byte[] decode(String rowKey) {
        return Base64.decode(replace(rowKey));
    }
}
